package org.zxp.designpattern.observer.java;

import java.util.ArrayList;

/**
 * @program: effectiveJava
 * @description: 被观察者
 * @author: X-Pacific zhang
 * @create: 2019-08-29 11:21
 **/
public class Watched {
    ArrayList<Watcher> watcherList = new ArrayList();

    public void addWatcher(Watcher watcher){
        watcherList.add(watcher);
    }

    public void change(){
        watcherList.forEach(o -> o.update());
    }


    public static void main(String[] args){
        Watched watched = new Watched();
        //添加观察者
        watched.addWatcher(new Watcher1());
        watched.addWatcher(new Watcher2());
        watched.addWatcher(() -> System.out.println("Wacther3被通知"));

        //主题/被观察者发生变更
        watched.change();
    }
}
